package com.gohi.main.wxapi.ctrl;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import org.springframework.web.servlet.ModelAndView;

/*import com.gohi.main.wxapi.service.WxApiProcess;*/
import com.gohi.main.wxapi.util.SignUtil;
/*import com.wxcms.domain.Account;
import com.wxcms.service.AccountService;*/


/**
 * 微信与开发者服务器交互接口
 * 如果是GET 请求，那么是进行 URL 、Tocken 认证；
 * 如果是POST 请求，那么是消息交互
 * @author brain
 */
@Controller
public class WxApiCtrl {
	/*@Autowired
	private WxApiProcess wxApiProcess;
	
	@Autowired
	private AccountService accountService;
	
	*//**
	 * GET请求：进行URL、Tocken 认证；
	 * 1. 将token、timestamp、nonce三个参数进行字典序排序
	 * 2. 将三个参数字符串拼接成一个字符串进行sha1加密
	 * 3. 开发者获得加密后的字符串可与signature对比，标识该请求来源于微信
	 * 这里可以添加多个账户，不同的account/tocken，提供给微信平台，只要验证通过即可
	 *//*
	@RequestMapping(value = "/{account}/wxapi",  method = RequestMethod.GET)
	public @ResponseBody String doGet(HttpServletRequest request,@PathVariable String account) {
//		System.out.println(" account = " + account);
		Account actTmp = accountService.getByAccount(account);//获取account
		if(actTmp != null){
			System.out.println(" actTmp.token = " + actTmp.getToken());
			String tocken = actTmp.getToken();//获取token，进行验证；
			
			String signature = request.getParameter("signature");// 微信加密签名
			String timestamp = request.getParameter("timestamp");// 时间戳
			String nonce = request.getParameter("nonce");// 随机数
			String echostr = request.getParameter("echostr");// 随机字符串
			// 校验成功返回  echostr，成功成为开发者；否则返回error，接入失败
			if (SignUtil.validSign(signature, tocken, timestamp, nonce)) {
				System.err.println(echostr);
				return echostr;
			}
		}
		return "error";
	}
	
	*//**
	 * POST 请求：进行消息处理；
	 * *//*
	@RequestMapping(value = "/{account}/wxapi", method = RequestMethod.POST)
	public @ResponseBody String doPost(HttpServletRequest request,@PathVariable String account,HttpServletResponse response) {
		System.out.println(" wxapi post ");
		//处理用户和微信公众账号交互消息
		Account actTmp = accountService.getDistinctAccount();//获取account
		String appId = null;
		String appSecret = null;
		if(actTmp != null){//订阅号（非认证）没有 appId 和  appSecret 
			appId = actTmp.getAppid();
			appSecret = actTmp.getAppsecret();
		}
		return wxApiProcess.processMsg(request,appId,appSecret);
	}
	
	//创建微信公众账号菜单
	@RequestMapping(value = "/wxapi/publishMenu")
	public ModelAndView publishMenu(HttpServletRequest request) {
		//处理用户和微信公众账号交互消息
		Account actTmp = accountService.getDistinctAccount();//获取account
		if(actTmp != null){
			boolean flag = wxApiProcess.publishMenu(actTmp.getAppid(),actTmp.getAppsecret());
			if(flag){
				ModelAndView mv = new ModelAndView("common/success");
				mv.addObject("successMsg", "创建菜单成功");
				return mv;
			}
		}
		ModelAndView mv = new ModelAndView("common/failure");
		mv.addObject("failureMsg", "创建菜单失败，请检查菜单：可创建最多3个一级菜单，每个一级菜单下可创建最多5个二级菜单。");
		return mv;
	}
	
	//删除微信公众账号菜单
	@RequestMapping(value = "/wxapi/deleteMenu")
	public ModelAndView deleteMenu(HttpServletRequest request) {
		//处理用户和微信公众账号交互消息
		Account actTmp = accountService.getDistinctAccount();//获取account
		if(actTmp != null){
			boolean flag = wxApiProcess.deleteMenu(actTmp.getAppid(),actTmp.getAppsecret());
			if(flag){
				ModelAndView mv = new ModelAndView("common/success");
				mv.addObject("successMsg", "删除菜单成功");
				return mv;
			}
		}
		ModelAndView mv = new ModelAndView("common/failure");
		mv.addObject("failureMsg", "删除菜单失败");
		return mv;
	}
	*/
}



